package org.ejde.sciencelogs;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * This class contains the attributes and behaviors for 
 * UDP-based logging client.
 *  
 * @author Eric Martin
 * @version 0.1
 */
public class UdpXmitter {

	// Constants
	final static private int buflength = 1024;

	/**
	 * The IP of the server
	 */
	private String targetIP;
	
	/**
	 * The port number of the server
	 */
	private int targetPort;
	
	/** 
	 * The Inet address of the server
	 */
	private InetAddress address;
	
	/**
	 * The socket object
	 */
	private DatagramSocket socket;
	
	
	private String fname;
	
	
	private String dlm;

	// public constants
	public enum LogType {
		LOCALLOG, REMOTELOG
	}

	private LogType opmode;

	// Variables for Local Logging

	/**
	 * Contructor method
	 * 
	 * @param targetIP IP of the server
	 * @param targetPort port number of the server
	 * @throws IOException
	 */
	public UdpXmitter(String targetIP, int targetPort) throws IOException {
		try {
			this.targetIP = targetIP;
			this.targetPort = targetPort;

			this.socket = new DatagramSocket();
			this.address = InetAddress.getByName(targetIP);
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.opmode = LogType.REMOTELOG;
	}

	/**
	 * Constructor method, used only when local logging is
	 * enabled.
	 * 
	 * @param targetIP the IP address of the server.
	 * @param targetPort the port number of the server.
	 * @param fname the filename of the file to transmit.
	 * @param dlm the field delimiter to use when
	 * transmitting the file.
	 * @throws IOException
	 */
	public UdpXmitter(String targetIP, int targetPort, String fname, String dlm)
			throws IOException {
		this.opmode = LogType.LOCALLOG;
		this.fname = fname;
		this.dlm = dlm;
		try {
			this.targetIP = targetIP;
			this.targetPort = targetPort;
			this.socket = new DatagramSocket();
			this.address = InetAddress.getByName(targetIP);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * Send the message to the UDP-based server.
	 * 
	 * @param msg text message to send.
	 * @throws IOException
	 */
	public void send(String msg) throws IOException {
		try {
			// Encode Data
			byte[] buf = new byte[buflength];
			buf = msg.getBytes();

			// Transmit Data
			DatagramPacket packet = new DatagramPacket(buf, buf.length,
					address, targetPort);
			socket.send(packet);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// If local logging is enabled, write the same
		// message to file.
		if (this.opmode == LogType.LOCALLOG && fname != null) {
			Date now = new Date();
			SimpleDateFormat fmt = new SimpleDateFormat(dlm + "D" + dlm
					+ "ddMMMyyyy" + dlm + "HH:mm:ss" + dlm);

			String logstr = Long.toString(now.getTime()) + fmt.format(now)
					+ msg + "\n";
			System.out.println(logstr);
			try {
				BufferedWriter logwriter = new BufferedWriter(new FileWriter(
						fname, true));
				logwriter.write(logstr);
				logwriter.close();
			} catch (IOException e) {
				e.printStackTrace();
				fname = null;
			}
		}
	}
}
